package two_xia.ex9.Demo07;

import java.util.ArrayList;
import java.util.List;

class AllyMember implements Subject {
    private List<Observer> allies;
    private String name;

    public AllyMember(String name) {
        this.name = name;
        allies = new ArrayList<>();
    }

    public void attach(Observer observer) {
        allies.add(observer);
    }

    public void detach(Observer observer) {
        allies.remove(observer);
    }

    public void notifyObservers(String message) {
        for (Observer ally : allies) {
            ally.update(message);
        }
    }

    public void beAttacked(String message) {
        System.out.println(name + "受到攻击！通知盟友：" + message);
        notifyObservers(message);
    }
}
